package news

import (
	"fmt"
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/news"

	"github.com/gin-gonic/gin"
)

func CategoryAllList(c *gin.Context) {

	var list []struct {
		ID   uint
		Name string
	}

	// 查询列表
	if err := bean.MysqlDb.Model(&news.NewsCategory{}).Order("`sort` ASC").Find(&list).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.PureJSON(200, gin.H{
		"list": list,
	})

}

func CategoryList(c *gin.Context) {

	var req struct {
		Page   int    `binding:"required,gte=1,max=10000000"`
		Limit  int    `binding:"required,gte=1,max=10000000"`
		Name   string `binding:"max=255"`
		IsShow string `binding:"oneof='' 是 否"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	var offset = (req.Page - 1) * req.Limit

	// 查询
	var count int64
	var list []news.NewsCategory

	var tx = bean.MysqlDb.Model(&news.NewsCategory{})

	if req.Name != "" {
		tx = tx.Where("name like ?", "%"+req.Name+"%")
	}

	if req.IsShow != "" {
		tx = tx.Where("is_show = ?", req.IsShow)
	}

	// 查询总数
	if err := tx.Count(&count).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 查询列表
	if err := tx.Limit(req.Limit).Offset(offset).Order("sort ASC").Find(&list).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.PureJSON(200, gin.H{
		"count": count,
		"list":  list,
	})
}

func CategoryEdit(c *gin.Context) {
	var req struct {
		ID     uint   `binding:"max=10000000"`
		Name   string `binding:"max=255"`
		Sort   uint   `binding:"max=10000"`
		IsShow string `binding:"oneof='' 是 否"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	// 查出现有对象
	var one news.NewsCategory
	one.ID = req.ID
	one.Name = req.Name
	one.Sort = req.Sort
	one.IsShow = req.IsShow

	// 保存对象
	if one.ID == 0 {
		if err := bean.MysqlDb.Create(&one).Error; err != nil {
			bean.Log.Error(err)
			c.String(500, "数据库错误")
			return
		}
	} else {
		if err := bean.MysqlDb.Updates(&one).Error; err != nil {
			bean.Log.Error(err)
			c.String(500, "数据库错误")
			return
		}
	}

	c.String(200, "ok")
}

func CategoryDetail(c *gin.Context) {
	var req struct {
		ID uint `binding:"max=10000000"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	var one news.NewsCategory

	if err := bean.MysqlDb.Model(&news.NewsCategory{}).Where("id=?", req.ID).First(&one).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, fmt.Sprintf("栏目不存在id:%d", req.ID))
		return
	}

	c.PureJSON(200, one)
}

func CategoryDelete(c *gin.Context) {
	var req struct {
		ID uint `binding:"max=10000000"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	// 检查是相关新闻是否都删除完了
	var newsCount int64
	if err := bean.MysqlDb.Model(&news.News{}).Where("id=?", req.ID).Count(&newsCount).Error; err != nil {
		bean.Log.Warning(err)
		return
	}

	if newsCount > 0 {
		c.String(400, fmt.Sprintf("请先清空该栏目下文章，总数：%d", newsCount))
		return
	}

	var one news.NewsCategory
	if err := bean.MysqlDb.Where("id=?", req.ID).First(&one).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, fmt.Sprintf("栏目不存在id:%d", req.ID))
		return
	}

	if err := bean.MysqlDb.Delete(&one).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.String(200, "ok")
}
